package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 *
 * @author msjykf
 * 适当性回访抽数
 *  {"number": 3}
 *  number:每个样本抽取数量;
 *
 */

public class OboundRecallSales implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(OboundRecallSales.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			def jsonbody = Util.getRequestBody(request,'utf-8'); //获取报文
			if(jsonbody.equals("")) {
				return "{\"msg\": \"未获取到入参报文！\",\"code\": -2}"
			};
			JSONObject obj = JSONObject.fromObject(jsonbody); //解析报文
			def number = Util.filterForObject(obj.get("number"));
			if(number.equals("")) {
				return "{\"msg\": \"number参数不能为空！\",\"code\": -2}"
			};
			def smap = eliteBean.chaxun_sql("select customgetid('OB') as batch_id from dual","dataSource");
			def	batch_id = smap[0].get("batch_id");
			eliteBean.update("insert into obtask_batch(batch_id,taskid,requeststate,procstate,recordcount)values('"+batch_id+"','LO3588',0,0,0)","dataSource");
			PropertiesUtil pt= new PropertiesUtil();
			def ibqurl = pt.readValue("ibqurl"); //获取webservice接口地址
			def appid = pt.readValue("appid"); //获取appId
			def charset = "utf-8"; //字符集
			def timeout = 100000; //请求超时时间
			def xmlbody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
							<SOAP-ENV:Body>
								<m:executeProcedure xmlns:m="http://service.web.server.ua.businessmatrix.com/">
									<appId>"""+appid+"""</appId>
									<name>s-P_RECALL_SALES</name>
									<params>
										<isOutDirectionParam>false</isOutDirectionParam>
										<name>I_BATCH_ID</name>
										<value>"""+batch_id+"""</value>
									</params>
									<params>
										<isOutDirectionParam>false</isOutDirectionParam>
										<name>I_CUST</name>
										<value>"""+number+"""</value>
									</params>
									</m:executeProcedure>
								</SOAP-ENV:Body>
							</SOAP-ENV:Envelope>""";
			def returnxml = Util.callWebService(ibqurl,xmlbody,charset,"",timeout);
			eliteBean.update("update obtask_batch set requeststate=1,requestdate=sysdate,requestcontent='"+xmlbody+"' where batch_id='"+batch_id+"'","dataSource");
			return "{\"msg\": \"请求接口成功！\",\"code\": 1}"
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			return "{\"msg\": \"请求接口失败！\",\"code\": -1}"
		}
	}
}